package com.armail.client.main;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import javax.ws.rs.core.MediaType;

import com.armail.client.dto.Mail;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class Test extends Application{
	
	Label statusLabel = new Label();
	TableView<Mail> mailTable = new TableView<Mail>();
	private static final String REST_URI = "http://localhost:8080/com-armail-server/";
	private static final String GET_MAILS = "/mails/getAllmails";
	private static final String SEND_MAIL = "/mails/send";
	
	
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public void start(Stage stage) throws Exception {
		ObservableList<Mail> mails = FXCollections.observableArrayList(getMails());
		mailTable.setItems(mails);
		
		VBox vbox = new VBox();
		vbox.setPrefSize(600, 600);
		vbox.setSpacing(10);
		vbox.setPadding(new Insets(10));
		
		mailTable.setPrefHeight(300);
		
		TableColumn<Mail, String> fromColumn = new TableColumn<>("From");
		fromColumn.setPrefWidth(100);
		fromColumn.setCellValueFactory(new PropertyValueFactory<Mail, String>(
				"fromUser"));
		
		TableColumn<Mail, String> toColumn = new TableColumn<>("To");
		toColumn.setPrefWidth(100);
		toColumn.setCellValueFactory(new PropertyValueFactory<Mail, String>(
				"toUser"));
		
		TableColumn<Mail, String> subjectColumn = new TableColumn<>("Subject");
		subjectColumn.setPrefWidth(100);
		subjectColumn.setCellValueFactory(new PropertyValueFactory<Mail, String>(
				"subject"));
		
		TableColumn<Mail, Boolean> hasAttachmentColumn = new TableColumn<>("Attachment");
		hasAttachmentColumn.setPrefWidth(100);
		hasAttachmentColumn.setCellValueFactory(new PropertyValueFactory<Mail, Boolean>(
				"hasAttachment"));
		
		TableColumn<Mail, String> recievedDateColumn = new TableColumn<>("Date");
		recievedDateColumn.setPrefWidth(100);
		recievedDateColumn.setCellValueFactory(new PropertyValueFactory<Mail, String>(
				"recievedDate"));
		
		mailTable.getColumns().addAll(fromColumn, toColumn, subjectColumn, hasAttachmentColumn, recievedDateColumn);
		
		
		Label mailsLabel = new Label("AR Mails");
		
		vbox.getChildren().addAll(mailsLabel, mailTable, getNewMailVBox());
		stage.setScene(new Scene(vbox));
		stage.show();
	}
	
	private void refreshTable() {
		mailTable.getItems().clear();
		mailTable.setItems(FXCollections.observableArrayList(getMails()));
	}
	
	private Node getNewMailVBox() {
		VBox newMailVBox = new VBox();
		newMailVBox.setSpacing(15);
		newMailVBox.setPrefWidth(200);
		
		final TextField toTextField = new TextField();
		toTextField.setMinHeight(25);
		toTextField.setPromptText("To");
		
		final TextField subjectTextFieldField = new TextField();
		subjectTextFieldField.setMinHeight(25);
		subjectTextFieldField.setPromptText("Subject");
		
		final TextArea bodyTextArea = new TextArea();
		bodyTextArea.setPromptText("Body");
		
		Button sendButton = new Button("Send");
		sendButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				Mail mail = new Mail();
				mail.setSubject(subjectTextFieldField.getText());
				mail.setRecievedDate(new Date());
				mail.setToUser(toTextField.getText());
				sendMail(mail);
				refreshTable();
				toTextField.clear();
				subjectTextFieldField.clear();
				bodyTextArea.clear();
			}
		});
		
		HBox bottonHBox = new HBox();
		bottonHBox.setSpacing(20);
		
		bottonHBox.getChildren().addAll(sendButton, statusLabel);
		
		newMailVBox.getChildren().addAll(toTextField, subjectTextFieldField, bodyTextArea, bottonHBox);
		return newMailVBox;
	}

	private  List<Mail> getMails() {
		ClientConfig config = new DefaultClientConfig();  
        Client client = Client.create(config);  
        WebResource resource = client.resource(REST_URI);  
        
        WebResource employeeResource = resource.path("/rest/").path(GET_MAILS); 
        final ClientResponse response = employeeResource.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
        return response.getEntity(new GenericType<List<Mail>>() {});
	}
	
	private void sendMail(Mail mail) {
		ClientConfig config = new DefaultClientConfig();  
        Client client = Client.create(config);  
        WebResource resource = client.resource(REST_URI);  
        
        WebResource employeeResource = resource.path("/rest/").path(SEND_MAIL); 
        final ClientResponse response = employeeResource.accept(MediaType.APPLICATION_XML).post(ClientResponse.class, mail);
        Mail sentMail = response.getEntity(Mail.class);
        statusLabel.setText("Mail sent successfully!! ");
	}

	public static void main(String[] args) {
		Application.launch(args);
	}
}